

//============= RECEIVE SERIAL COMMANDS =========================
//
// NOTE: - NUMERIC PARAMETERS have to be send BEFORE the command word. They must be sent as ASCII DEC, without end character. 
//       - Commands words SHOULD NOT have numbers in it. They should be C compliant STRINGS (ended with character '0')
//       - order is irrelevant: we can send 10 RADIUS or RADIUS 10. 

// String to store ALPHANUMERIC DATA (i.e., integers, floating point numbers, unsigned ints, etc represented as DEC) sent wirelessly: 
char stringData[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
int indexStringData=0;

// String to store COMMAND WORDS:
char stringCommand[24]; // note: an integer is two bytes long, represented with a maximum of 5 digits, but we may send floats or unsigned int...
int indexStringCommand=0;
boolean commandReady=false; // will become true when receiving the byte 0 (i.e. the '/0' string terminator)

void processSerial() {
   // read the incoming byte:
  int val = Serial.read();
  
  // Save ASCII numeric characters (ASCII 0 - 9) on stringData:
    if ((val >= '0') && (val <= '9')){ // this is 45 to 57 (included)
      stringData[indexStringData] = val;
      indexStringData++;
    }
    
  // Save ASCII letters in stringCommand:
    if ((val >= 'A') && (val <= 'z')){ // this is 65 to 122 (included)
      stringCommand[indexStringCommand] = val;
      indexStringCommand++;
    }
   // is command ready?
   if (val=='/') {
     commandReady=true; 
     stringCommand[indexStringCommand] = 0; // string termination.
     indexStringCommand=0; // reset index string for acquiring next command
     //Serial.println(stringCommand);
   }
    
    // COMMANDS (with or without numeric parameters):
    if (commandReady==true) { // it means we can interpret the command string:
      commandReady=false;
     
      stringData[indexStringData] = 0 ;// string termination for numeric values;
      indexStringData=0;
     
     // Parse command (get address[0] and address[1]):
     //ex:  "/1/standBy" -- > address[0]="1" and address[1]="standBy"
    // address[2]
     
    // Serial.println(stringCommand);
    // Serial.println(stringData);
     
     
     // Parse data:
     
   //  char address[2][24];
//long auxdata[2]; // to store a max of two arguments (note: we will only use LONGs)
//int data[2]; // this is to have -1 as NO DATA, to detect errors. 

     // FOR THE TIME BEING:
     if (!strcmp(stringCommand , "takeSnapshot")) {
      IO.scanArea_Serial(atoi(stringData));
     } 
     
      
    }
    
 }
 
 
